package com.start.core.log;

import com.start.common.std.StartBaseSingleton;
import com.start.common.std.StartSingletonFactory;
import com.start.common.tools.SpringContextTools;
import com.start.core.entity.StartLogEntity;
import com.start.core.service.StartLogService;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 日志存储器
 */
public class StartLogStore extends StartBaseSingleton<StartLogStore> {

    private ExecutorService executorService;
    private StartLogService startLogService;


    public void init() {
        executorService = Executors.newFixedThreadPool(10);
        startLogService = SpringContextTools.getBean(StartLogService.class);
    }

    public static StartLogStore getStore() {
        return StartSingletonFactory.get(StartLogStore.class);
    }

    public void save(StartLogEntity log) {
        if (log.getCreateTime() == null) {
            log.setCreateTime(LocalDateTime.now());
        }
        executorService.submit(() -> {
            startLogService.save(log);
        });
    }

    public void close() {
        executorService.shutdown();
        executorService = null;
        startLogService = null;
    }
}
